<?php
namespace app\admin\service;
use app\model\Order;
use app\model\User;
use app\model\Collect;
use app\model\Goods;
use app\model\OrderLog;

class OrderService{
    public function index($param,$where = '1 = 1')
    {
        $orderModel = new Order;
        $orders = $orderModel::with('goods')->where($where)->order('add_time desc')->paginate(15,false,['query'=>$param]);
        $page = $orders->render();
        return ['orders'=>$orders,'page'=>$page];
    }

    public function orderBalance($param)
    {
        // $userModel = new User;
        // $users = $userModel::order('add_time desc')->paginate(15,false,['query'=>$param]);
        // $page = $users->render();
        // return ['users'=>$users,'page'=>$page];
        $orderLogModel = new OrderLog;
        $orderLogs = $orderLogModel::with('user,goods')->where('counts','neq',0)->order('add_time desc')->paginate(15,false,['query'=>$param]);
        // dump($orderLogs);die;
        $page = $orderLogs->render();
        return ['orderLogs'=>$orderLogs,'page'=>$page];
    }

    public function myGoods($param)
    {
        $uid = $param['uid'];
        // dump($uid);die;
        $collectModel = new Collect;
        $collects = $collectModel::with('goods')->where(['uid'=>$uid,'status'=>1])->select();
        return ['collects'=>$collects];
        // dump($collects);die;
    }

    public function lookOrder($param)
    {
        $where['uid'] = $param['uid'];
        $where['tk_status'] = 3;
        if(array_key_exists('status',$param)){
            if($param['status'] != 3){
                $where['status'] = $param['status'];
            }
        }
        $goodsModel = new Goods;
        $where['num_iid'] = $goodsModel::where('id',$param['goods_id'])->value('sp_id');
        // dump($where);die;
        $orderModel = new Order;
        $orders = $orderModel::with('goods,user')->where($where)->paginate(15,false,['query'=>$param]);
        // dump($orders);die;
        $page = $orders->render();
        return ['orders'=>$orders,'page'=>$page,'uid'=>$param['uid'],'goods_id'=>$param['goods_id']];
    }

    public function balanceOrder($post)
    {
        // dump($post);die;
        $ids = $post['ids'];
        $uid = $post['uid'];
        $orderModel = new Order;
        $orders = $orderModel::where('id','in',$ids)->select();
        $goodsModel = new Goods;
        $goods = $goodsModel::find($post['goods_id']);
        // dump($orders);die;
        $have_balance_order = $banlace = [];//$have_balance_order:存放不能结算的订单，$banlace：存放每个订单的佣金
        foreach($orders as $k=>$v){
            if($v->status != 1){
                $have_balance_order[] = $v->trade_id;
            }else{
                $banlace[] = $v->item_num*$goods->money*$goods->commision/100;
            }
        }
        // dump($banlace);die;
        if(!empty($have_balance_order)){
            return ['code'=>-1,'msg'=>'订单不能结算','result'=>implode(',',$have_balance_order)];
        }
        // dump(array_sum($banlace));die;
        $info_order = $orderModel::where('id','in',$ids)->update(['status'=>2]);
        if(!$info_order){
            return ['code'=>-2,'msg'=>'系统繁忙'];
        }
        $orderLogModel = new OrderLog;
        $info_order_log = $orderLogModel::where(['uid'=>$uid,'goods_id'=>$post['goods_id']])->where('counts','neq',0)->setDec('counts',count($banlace));
        if(!$info_order_log){
            $orderModel::where('id','in',$ids)->update(['status'=>1]);
            return ['code'=>-3,'msg'=>'系统繁忙'];
        }

        $userModel = new User;
        $info_user = $userModel::where('id',$uid)->setInc('balance',array_sum($banlace));
        if(!$info_user){
            $orderModel::where('id','in',$ids)->update(['status'=>1]);
            $orderLogModel::where(['uid'=>$uid,'goods_id'=>$post['goods_id']])->where('counts','neq',0)->setInc('counts',count($banlace));
            return ['code'=>-4,'msg'=>'系统繁忙'];
        }
        return ['code'=>1,'msg'=>'已结算'];
    }
}
